package com.tanhua.app.controller;

import com.tanhua.app.interceptor.UserHolder;
import com.tanhua.app.service.CommentsService;
import com.tanhua.app.service.MovementService;
import com.tanhua.model.mongo.Movement;
import com.tanhua.model.vo.MovementsVo;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.VisitorsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/movements")
public class MovementController {

    @Autowired
    private MovementService movementService;

    @Autowired
    private CommentsService commentsService;

    //发布动态
    @PostMapping
    public ResponseEntity sendmovements(Movement movement, MultipartFile imageContent[]) throws IOException {
        movementService.sendmovements(movement, imageContent);
        return ResponseEntity.ok(null);
    }

    //查询个人的动态
    @GetMapping("/all")
    public ResponseEntity findByUserId(Long userId
            , @RequestParam(defaultValue = "1") Integer page
            , @RequestParam(defaultValue = "10") Integer pagesize) {
        PageResult<MovementsVo> pr = movementService.findByUserId(userId, page, pagesize);
        return ResponseEntity.ok(pr);
    }

    //查询好友动态
    @GetMapping
    public ResponseEntity findFriendsMovements(@RequestParam(defaultValue = "1") Integer page
            , @RequestParam(defaultValue = "10") Integer pagesize) {
        PageResult<MovementsVo> pr = movementService.findFriendsMovements(page, pagesize);
        return ResponseEntity.ok(pr);
    }

    //查询推荐动态
    @GetMapping("/recommend")
    public ResponseEntity recommend(@RequestParam(defaultValue = "1") Integer page
            , @RequestParam(defaultValue = "10") Integer pagesize){
        PageResult pr = movementService.recommend(page, pagesize);
        return ResponseEntity.ok(pr);
    }

    //查询单条动态
    @GetMapping("/{id}")
    public ResponseEntity findById(@PathVariable("id") String movementId) {
        MovementsVo vo = movementService.findById(movementId);
        return ResponseEntity.ok(vo);
    }

    //动态点赞
    @GetMapping("/{id}/like")
    public ResponseEntity like(@PathVariable String id){
        Integer likeCount = commentsService.like(id);
        return ResponseEntity.ok(likeCount);
    }

    //取消动态点赞
    @GetMapping("/{id}/dislike")
    public ResponseEntity dislike(@PathVariable String id){
        Integer likecount = commentsService.dislike(id);
        return ResponseEntity.ok(likecount);
    }

    //动态喜欢
    @GetMapping("/{id}/love")
    public ResponseEntity love(@PathVariable String id){
        Integer loveCount = commentsService.love(id);
        return ResponseEntity.ok(loveCount);
    }

    //取消动态喜欢
    @GetMapping("/{id}/unlove")
    public ResponseEntity unlove(@PathVariable String id){
        Integer loveCount = commentsService.unlove(id);
        return ResponseEntity.ok(loveCount);
    }

    //谁看过我
    @GetMapping("/visitors")
    public ResponseEntity visitors(){
        List<VisitorsVo> vos = movementService.visitors(UserHolder.getUserId());
        return ResponseEntity.ok(vos);
    }
}
